package com.moon.game.command.guest.lair;

import java.util.LinkedList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.exception.GException;
import com.moon.game.service.LairService;
import com.moon.game.util.Utils;
import com.moon.game.vos.RentLairBean;

/**
 * @description 展示所有出租的孵化塔
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-2 下午6:13:37
 */
public class ShowRentingLairsCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}		
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		//0查看凡兽1查看仙兽2查看神兽-1查看全部
		int mode = Integer.parseInt(ps[0]);
		int page = Integer.parseInt(ps[1]);
		LinkedList<RentLairBean> rentingLairs = LairService.rentingLairsFilter(LairService.getRentingLairs(), mode);
		LinkedList<RentLairBean> pagedRentingLairs = new LinkedList<RentLairBean>();
		List<Object> objs = Utils.page(rentingLairs, page, Constants.PAGE_SIZE);
		for (Object obj : objs) {
			//分页
			RentLairBean bean = (RentLairBean)obj;
			//过滤自己
			if (req.getPlayer().getSid().equals(bean.getSid()) && req.getPlayerId().equals(bean.getPid())) {
				continue;
			}
			pagedRentingLairs.add(bean);
		}
		
		if (rentingLairs != null && rentingLairs.size() > 0) {
			req.setVO("total_page", Utils.totalPage(rentingLairs.size(), Constants.PAGE_SIZE));
		}
		req.setVO("paged_renting_lairs", pagedRentingLairs);
		req.setVO("curr_mode", mode);
		req.setVO("curr_page", page);
		response(req, req.getClientResponse());
	}

}
